﻿// Copyright (c)  MOKEYISH. All rights reserved.
// Licensed under the MIT License, See License.txt in the project root for license information.

using System;
using System.Collections.Generic;

namespace NSQL.TypeMapping
{
    public class NpgsqlTypeMaps
    {
        private static readonly List<TypeMap> List = new List<TypeMap>();
        public static readonly IReadOnlyList<TypeMap> Types = List.AsReadOnly();

        public static readonly TypeMap Boolean = NewType("boolean", new[] {DbType.Boolean}, new[] {typeof(bool)},
            DbType.Boolean);

        public static readonly TypeMap SmallInt = NewType("smallint", new[] {DbType.Int16}, new[] {typeof(short)},
            DbType.Int16);

        public static readonly TypeMap Integer = NewType("integer", new[] {DbType.Int32}, new[] {typeof(int)},
            DbType.Int32);

        public static readonly TypeMap BigInt = NewType("bigint", new[] {DbType.Int64}, new[] {typeof(long)},
            DbType.Int64);

        public static readonly TypeMap DoublePrecision = NewType("double precision", new[] {DbType.Double},
            new[] {typeof(double)}, DbType.Double);

        public static readonly TypeMap Numeric = NewType("numeric",
            new[] {DbType.Decimal, DbType.Single, DbType.Double, DbType.Int32},
            new[] {typeof(decimal), typeof(float), typeof(double)}, DbType.Decimal);

        public static readonly TypeMap Money = NewType("money", new[] {DbType.Currency}, new[] {typeof(decimal)},
            DbType.Currency);

        public static readonly TypeMap Text = NewType("text", new[] {DbType.String}, new[] {typeof(string)},
            DbType.String);

        public static readonly TypeMap CharacterVarying = NewType("character varying", new[] {DbType.String},
            new[] {typeof(string)}, DbType.String);

        public static readonly TypeMap Character =
            NewType("character", new[] {DbType.String}, new[] {typeof(string)}, DbType.String);

        public static readonly TypeMap CiText = NewType("citext", new[] {DbType.String}, new[] {typeof(string)},
            DbType.String);

        public static readonly TypeMap Json = NewType("json", new[] {DbType.String}, new[] {typeof(string)},
            DbType.String);

        public static readonly TypeMap Jsonb = NewType("jsonb", new[] {DbType.String}, new[] {typeof(string)},
            DbType.String);

        public static readonly TypeMap Xml = NewType("xml", new[] {DbType.String}, new[] {typeof(string)},
            DbType.String);

        public static readonly TypeMap Uuid = NewType("uuid", new[] {DbType.Guid}, new[] {typeof(Guid)}, DbType.Guid);

        public static readonly TypeMap Date = NewType("date", new[] {DbType.Date}, new[] {typeof(DateTime)},
            DbType.Date);

        public static readonly TypeMap Interval = NewType("interval", new[] {DbType.DateTimeOffset},
            new[] {typeof(TimeSpan)}, DbType.DateTimeOffset);

        public static readonly TypeMap Timestamp = NewType("timestamp", new[] {DbType.DateTimeOffset},
            new[] {typeof(TimeSpan)}, DbType.DateTimeOffset);

        public static readonly TypeMap Time = NewType("time",  new[] {DbType.Time},
            new[] {typeof(TimeSpan)}, DbType.Time);
        public static readonly TypeMap Bit1 = NewType("bit(1)", new []{DbType.Binary}, new []{typeof(byte)}, DbType.Binary);


        private static TypeMap NewType(string typeName, DbType[] dbTypes, Type[] clrTypes, DbType? inferredDbType)
        {
            var t = new TypeMap(typeName, dbTypes, clrTypes, inferredDbType);
            List.Add(t);
            return t;
        }
    }
}